package net.jlibrary.student.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.student.action.vo.StudentVo;
import net.jlibrary.student.service.IStudentManager;

@RequestMapping("/student")
@RestController
public class StudentAction {

	@Autowired
	private IStudentManager studentMapper;

	// 目录页
	@RequestMapping(value = "/studentView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/student/studentView");
	}

	// 查询学生信息列表
	@RequestMapping(value = "/selectStudentList", method = { RequestMethod.POST })
	public Pagination studentData(StudentVo studentVo, PaginationParam param) {
		Pagination pagination = studentMapper.listsPage(studentVo, param);
		/*
		 * SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 * List<StudentVo> books = studentMapper.lists(new StudentVo()); List<ModelMap>
		 * maps = new ArrayList<ModelMap>(); for (StudentVo student : books) { ModelMap
		 * map = new ModelMap(); map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE,
		 * student.getPrimaryKey()); map.put("name", student.getText()); map.put("age",
		 * student.getAge()); map.put("sex", student.getSex()); map.put("creatData",
		 * df.format(student.getCreatedate())); map.put("lastData",
		 * df.format(student.getLastdate())); maps.add(map); }
		 */
		return pagination;
	}

	// 表单初始化信息
	@RequestMapping(value = "/studentFormInit", method = { RequestMethod.GET })
	public StudentVo studentFormInit() {
		StudentVo studentVo = new StudentVo();
		return studentVo;
	}

	// 保存学生信息
	@RequestMapping(value = "/saveStudent", method = { RequestMethod.POST })
	public ModelMap saveStudent(@ModelAttribute("vm") StudentVo student) {
		studentMapper.save(student);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 详细单个学生信息
	@RequestMapping(value = "/detailStudent", method = { RequestMethod.GET })
	public StudentVo detail(StudentVo student) {
		student = studentMapper.get(student);
		if (student.getSex() == 1) {
			student.setFromSex("男");
		} else {
			student.setFromSex("女");
		}
		return student;
	}

	// 删除学生信息
	@RequestMapping(value = "/deleteStudent", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		if (!StringUtils.isNotBlank(id.toString())) {
			return null;
		}
		StudentVo studentVo = new StudentVo();
		studentVo.setId(id);
		studentMapper.delete(studentVo);
		ModelMap modelMap = new ModelMap();
		modelMap.put("success", true);
		return modelMap;
	}

	// 修改数据
	@RequestMapping(value = "/updateStudent", method = { RequestMethod.POST })
	public ModelMap update(@ModelAttribute("vm") StudentVo studentVo) {
		studentMapper.update(studentVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/renewStudent", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		StudentVo student = new StudentVo();
		student.setId(id);
		studentMapper.renew(student);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/sexData", method = { RequestMethod.GET })
	public List<ModelMap> sexData() {
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (int i = 0; i < 2; i++) {
			ModelMap map = new ModelMap();
			if (i == 0) {
				map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, "1");
				map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, "男");
				maps.add(map);
			}
			if (i == 1) {
				map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, "2");
				map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, "女");
				maps.add(map);
			}
		}
		return maps;
	}

}
